package com.appspot.itunegwt.client.mvp;

import java.util.Comparator;
import java.util.List;


import com.appspot.itunegwt.client.Resources.TableResource;
import com.appspot.itunegwt.shared.Tasks;
import com.gwtplatform.mvp.client.ViewImpl;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.inject.Inject;

public class DefaultView extends ViewImpl implements DefaultPresenter.MyView {

	private final Widget widget;
	private List<Tasks> taskLists;
	private CellTable<Tasks> tasks;
	private TextColumn<Tasks> taskNameColumn;
	private TextColumn<Tasks> commentColumn;
	private TextColumn<Tasks> checkBoxColumn;
	private TextColumn<Tasks> skillColumn;
	private TextColumn<Tasks> authorColumn;
	private TextColumn<Tasks> timeColumn;

	public interface Binder extends UiBinder<Widget, DefaultView> {
	}
	
	@UiField HTMLPanel content;
	@Inject
	public DefaultView(final Binder binder) {
		widget = binder.createAndBindUi(this);
		createCellTable();
		sortColumn();
		addTask("Ui Concept and Design", "6:00", "Quynh Nguyen", "UiBinder, Celltable, ListDataProvider", "Fun stuff! But it will take" +
				" a while to mimic iTune with my not-so-pro graphic skill haha. Done! Took a while to learn how to overwrite standard GWT CSS." +
				" This web app is compatible with all screen resolution!!!! It was quite a feast to make it work on all screen resolution", "✓");
		addTask("Make Table Colum Sortable", "0:45", "Quynh Nguyen", "ListHandler", "Took longer than I expected", "✓");
		addTask("Add Drop Down Items For Menu Item", "0:00", "Quynh Nguyen", "None", "Not started", "");
	}

	@Override
	public Widget asWidget() {
		return widget;
	}
	
	/*
	 * I KNOW I KNOW!! TOo much duplicate code lol
	 * I could've make a local class implement HasCell interface!! 
	 */
	public void createCellTable(){
	
		TableResource resource = GWT.create(TableResource.class);
		
		//Populate the Celltable
		tasks = new CellTable<Tasks>(10, resource);
		tasks.setWidth("100%");
		tasks.setHeight("100%");
		
		//Create a checkbox column
		checkBoxColumn = new TextColumn<Tasks>(){
			public String getValue(Tasks task){
				return task.getIsDone();
			}
		};
		checkBoxColumn.setSortable(true);
		
		//Create a task name column
		taskNameColumn = new TextColumn<Tasks>(){
			public String getValue(Tasks task){
				return task.getTask_name();
			}
		};
		taskNameColumn.setSortable(true);
		
	
		
		//Create a completion time column
		timeColumn = new TextColumn<Tasks>(){
			public String getValue(Tasks task){
				return task.getCompletionTime();
			}
		};
		timeColumn.setSortable(true);
		
		//Create a author name column
		authorColumn = new TextColumn<Tasks>(){
			public String getValue(Tasks task){
				return task.getAuthor();
			}
		};
		authorColumn.setSortable(true);
		
		//Create a skill learned column
		skillColumn = new TextColumn<Tasks>(){
			public String getValue(Tasks task){
				return task.getSkillLearned();
			}
		};
		skillColumn.setSortable(true);
		
		//Create a author name column
		commentColumn = new TextColumn<Tasks>(){
			public String getValue(Tasks task){
				return task.getDifficulty();
			}
		};
		commentColumn.setSortable(true);

		//Add Column To Table
		tasks.addColumn(checkBoxColumn, "✓");
		tasks.addColumn(taskNameColumn, "Task Name");
		tasks.addColumn(timeColumn, "Time");
		tasks.addColumn(authorColumn, "Author");
		tasks.addColumn(skillColumn, "Skill Learned");
		tasks.addColumn(commentColumn, "Comment");
		
		//Create List Data Provider To Populate the List
		ListDataProvider<Tasks> dataProvider = new ListDataProvider<Tasks>();
		
		//Connect the list data provider to the table
		dataProvider.addDataDisplay(tasks);
		
		//Add the list of item into list data provider which will go into the table
		//taskLists is a pointer in this case
		taskLists = dataProvider.getList();
		
		
		//Add table to panel
		content.add(tasks);
	}
	
	public void sortColumn(){
		 
		
	    ListHandler<Tasks> columnSortHandler = new ListHandler<Tasks>(taskLists);
	    
	    //Sort Task Name Column
	    columnSortHandler.setComparator(taskNameColumn, new compareMe("task name"));
	    tasks.addColumnSortHandler(columnSortHandler);
	    
	  //Sort Check box Column
	    columnSortHandler.setComparator(checkBoxColumn, new compareMe("checkbox"));
	    tasks.addColumnSortHandler(columnSortHandler);
	    
	  //Sort Time Column
	    columnSortHandler.setComparator(timeColumn, new compareMe("time"));
	    tasks.addColumnSortHandler(columnSortHandler);
	    
	  //Sort Difficulty Column
	    columnSortHandler.setComparator(commentColumn, new compareMe("difficulty"));
	    tasks.addColumnSortHandler(columnSortHandler);
	    
	  //Sort Skill Column
	    columnSortHandler.setComparator(skillColumn, new compareMe("skill"));
	    tasks.addColumnSortHandler(columnSortHandler);
	    
	  //Sort Time Column
	    columnSortHandler.setComparator(authorColumn, new compareMe("author"));
	    tasks.addColumnSortHandler(columnSortHandler);
	    
	    tasks.getColumnSortList().push(timeColumn);


	}
	
	//Add static task to the list
	public void addTask(String task_name, String completionTime, String author,
			String skillLearned, String difficulty, String isDone){
		
		taskLists.add(new Tasks(task_name, completionTime, author, skillLearned, difficulty, isDone ));
	}
	

	public class compareMe implements Comparator<Tasks>{
		
		String value;
		
		public compareMe(String value){
			this.value = value;
		}

		@Override
		public int compare(Tasks task1, Tasks task2){
			//Return 0 if they are refer to the same object
			if(task1 == task2){
				return 0;
			}
			//Deep comparison
			if(task1 != null){
				if(task2!=null){
					if(value.equals("checkbox")){
						return task1.getIsDone().compareTo(task2.getIsDone());
					}else if(value.equals("task name")){
						return task1.getTask_name().compareTo(task2.getTask_name());
					}else if(value.equals("time")){
						return task1.getCompletionTime().compareTo(task2.getCompletionTime());
					}else if(value.equals("author")){
						return task1.getAuthor().compareTo(task2.getAuthor());
					}else if(value.equals("skill")){
						return task1.getSkillLearned().compareTo(task2.getSkillLearned());
					}else if(value.equals("difficulty")){
						return task1.getDifficulty().compareTo(task2.getDifficulty());
					}
				}else{
					return 1;
				}
			}
			
			return -1;
		}

		
	}
	
	


}
